﻿jQuery.toSpace = function (input) {
    if (input != null) {
        return input.replace("%20", " ");
    }
    return input;
};
jQuery.isNullOrEmpty = function (input) {
    if (input != null && input != "") {
        return false;
    }
    return true;
};
jQuery.getDate = function (yearDiff, monthDiff, dayDiff) {
    var date = new Date();
    if (yearDiff != null) {
        date.setYear(date.getFullYear() + yearDiff);
    }
    if (monthDiff != null) {
        date.setMonth(date.getMonth() + monthDiff);
    }
    if (dayDiff != null) {
        date.setDate(date.getDate() + dayDiff);
    }

    var seperator1 = "-";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
    return currentdate;
};

jQuery.getCurrentDate = function () {
    var date = new Date();
    var seperator1 = "-";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
    return currentdate;
};
jQuery.getCurrentDateTime = function () {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
    return currentdate;
};
jQuery.prototype.serializeObject = function () {
    var obj = new Object();
    $.each(this.serializeArray(), function (index, param) {
        if (!(param.name in obj)) {
            obj[param.name] = param.value;
        }
    });
    return obj;
};
jQuery.prototype.serializeForm = function () {
    var result = new Object();
    $.each(this.find("input"), function (ix, obj) {
        var name = obj.name;
        if (name != null && name.length > 0) {
            var typeName = obj.type;
            if (typeName == "checkbox") {
                result[name] = obj.checked;
            } else if (typeName == "radio") {
                result[name] = obj.checked;
            } else {
                result[name] = obj.value;
            }
        }
    });
    $.each(this.find("select"), function (ix, obj) {
        var name = obj.name;
        if (name != null && name.length > 0) {
            result[name] = $(obj).val();
        }
    });
    return result;
};
jQuery.prototype.serializeParam = function () {
    var result = this.serialize();
    $.each(this.find("input[type='checkbox']"), function (ix, obj) {
        var name = obj.name;
        if (name != null && name.length > 0) {
            var oldStr = "&" + name + "=on";
            var newStr = "&" + name + "=" + obj.checked;
            result = result.replace(oldStr, "") + newStr;
        }
    });
    return result;
};
jQuery.downloadFile = function (url, method, fileName) {
    jQuery('<form action="' + url + '" method="' + (method || 'post') + '">' +
                '<input type="text" name="fileName" value="' + fileName + '"/>' +
            '</form>')
    .appendTo('body').submit().remove();
};

/**
 * 
 * @param {} indexStr  例如:",1,2,"
 * @returns {} 
 */
jQuery.prototype.makeTableFilter = function (indexStr) {
    if ($(this).find("tbody tr").length > 0) {
        var cellLength = $(this).find("tbody tr:eq(0) td").length;
        var line = "<tr>";
        for (var i = 0; i < cellLength; i++) {
            if (indexStr.indexOf("," + i + ",") > -1) {
                line += "<td></td>";
            } else {
                var ctl = "<input class='form-control' name='filter_" + i + "' type='text'>";
                line += "<td>" + ctl + "</td>";
            }
        }
        line += "</tr>";

        $(line).insertBefore($(this).find("tbody tr:eq(0)"));
        //绑定过滤时事件
        var $tableObj = $(this);
        $(this).on("input", "input[name^=filter_]", function () {
            var $filterInputs = $tableObj.find("tbody input[name^=filter_]");
            $tableObj.find("tbody tr:gt(0)").each(function (rowIndex, rowItem) {
                var isShow = true;
                //几个条件结合判断
                $filterInputs.each(function (ix, filterInput) {
                    var filterValue = $(filterInput).val();
                    var cellIndex = $(filterInput).prop("name").replace("filter_", "");
                    var content = $(rowItem).find("td:eq(" + cellIndex + ")")[0].innerHTML;
                    if (content.indexOf(filterValue) == -1) {
                        isShow = false;
                        return;
                    }
                });

                if (isShow) {
                    $(rowItem).show();
                } else {
                    $(rowItem).hide();
                }
            });

        });
    }
};